package org.example.controller;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.config.BResult;
import org.example.domain.MesMaterial;
import org.example.service.MesMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
public class MaterialController {

    @Autowired
    MesMaterialService mesMaterialService;

    @RequestMapping("/add")
    public BResult add(@RequestBody @Valid MesMaterial material) {
        String mNumber = "YCL"+IdUtil.getSnowflakeNextIdStr();
        material.setMNumber(mNumber);
        mesMaterialService.save(material);
        return BResult.success("success");
    }

    @RequestMapping("/list")
    public BResult list(MesMaterial query, @RequestParam(defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<MesMaterial> list = mesMaterialService.listPage(query);
        PageInfo<MesMaterial> pageInfo = new PageInfo<>(list);
        return BResult.success(pageInfo);
    }

}
